// const flyimages = require('./images.js');
// const emitter = require('../utils/emitter.js');
const app = getApp()
let unit = 0
let width = 0
let vh = 0
let isEnd = 0
let model = false
let isIos = false
// var isEnd = 0
let redNum = 0
let goldNum = 0
// 分数、红包数、金币数
let score = 0
let red_number = 0
let gold_number = 0
var audios = null
var audios_url = 'https://resource.160nurse.com/canvas/healthy/mp3/bgyy.mp3'
let audio_url = 'https://resource.160nurse.com/canvas/healthy/mp3/click.mp3'
let getgold = 'https://resource.160nurse.com/canvas/healthy/mp3/get_gold.mp3'
let zidan = 'https://resource.160nurse.com/canvas/healthy/mp3/strength.mp3'
let getbomb = 'https://resource.160nurse.com/canvas/healthy/mp3/getbomb.mp3'
let red = 'https://resource.160nurse.com/canvas/healthy/mp3/red.mp3'
// boss出现
let boss = 'https://resource.160nurse.com/canvas/healthy/mp3/boss.mp3'
// // var down_file = ''
let click_url = ''  // 子弹打中
let gold_url = '' // 捡到金币
let bg_url = '' //背景音效
// 捡到子弹音效
let zidan_url = ''
let bomb_url = ''
let boss_url = ''
let red_url = ''

function playBg() {
  let audio = wx.createInnerAudioContext()
  audios = audio
  audios.src = bg_url
  audios.autoplay = true
  audios.onPlay(() => {

  })
  audios.onEnded(() => {
    console.log('播放结束')
    audios.play()
  });
}
function playAudio () {
  let audio = wx.createInnerAudioContext()
  audio.src = click_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}
function getGolds () {
  let audio = wx.createInnerAudioContext()
  audio.src = gold_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}
function getDan() {
  let audio = wx.createInnerAudioContext()
  audio.src = zidan_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}
function getBomb() {
  let audio = wx.createInnerAudioContext()
  audio.src = bomb_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}
function getBoss() {
  let audio = wx.createInnerAudioContext()
  audio.src = boss_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}
function getRed() {
  let audio = wx.createInnerAudioContext()
  audio.src = red_url
  audio.autoplay = true
  audio.onPlay(() => {

  })
  audio.onEnded(() => {
    console.log('播放结束')
    audio.destroy()
  });
}

const flyimages = require('./images.js');
const emitter = require('../utils/emitter.js');

//游戏配置
var config = {
  "gameSpeed": model ?  4 : 8,  //游戏速度
  "cartridgeSpeed": 10  //子弹速度
};

function flyGame(opts) {
  var c_width = this.c_width = opts.width;
  var c_height = this.c_height = opts.height;  //画布的高和宽
  var cxt = this.cxt = opts.ctx;
  var id = this.id = opts.id;
  this.cxt.setFontSize(30);
  this.cxt.setFillStyle("#333");
  //等待时间
  var loadingTime = 0;

  //等待动画刷新事件
  var refresh = function () {
    // console.log('我来自fresh')
    drawBg();
    // drawLogo();
    load();
    loadingTime++;
    wx.drawCanvas({
      canvasId: id,
      actions: cxt.getActions()
    })
  }

  //设置背景
  function drawBg() {
    score = 0
    red_number = 0
    gold_number = 0
    var bg_img = flyimages["bg"];
    var bg_img_width = bg_img.width;
    var bg_img_height = bg_img.height;
    cxt.drawImage(bg_img.src, 0, 0, bg_img_width, bg_img_height);
    wx.getSystemInfo({
      success: function (res) {
        width = res.screenWidth,
          unit = width / 100,
          vh = res.screenHeight / 100,
          model = (res.model.indexOf("iPhone 6") > -1) || res.model.indexOf("iPhone 5") > -1 || res.model.indexOf("iPhone 4") > -1 ? true : false,
          isIos = res.model.indexOf("iPhone") > -1 ? true : false
      }
    })    
  }

  //构造logo
  // function drawLogo() {
  //   var logo_img = flyimages["logo"];
  //   var logo_width = logo_img.width;
  //   var logo_height = logo_img.height;

  //   var y = 100;
  //   cxt.drawImage(logo_img.src, 0, y, logo_width, logo_height);
  // }

  //等待动画
  function load() {
    wx.showLoading({
      title: '资源加载中'
    })
    if (loadingTime == 100) {
      loadingTime = 0;
      clearInterval(loadingClock);
      app.request(app.apiDomain + 'index.php?c=games&a=get_plane', {}, function (res) {
        if (res.data.status == 1) {
          redNum = res.data.data.redNum
          goldNum = res.data.data.goldNum
        }
        wx.downloadFile({
          url: audio_url,
          success: function (res) {
            click_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: getgold,
          success: function (res) {
            gold_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: boss,
          success: function (res) {
            boss_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: red,
          success: function (res) {
            red_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: getbomb,
          success: function (res) {
            bomb_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: zidan,
          success: function (res) {
            zidan_url = res.tempFilePath
            // game.start();
          }
        })
        wx.downloadFile({
          url: audios_url,
          success: function (res) {
            wx.hideLoading()
            bg_url = res.tempFilePath
            playBg()
            game.start();
          }
        })
      }, 'POST')
    }
  }

  //开始动画
  var loadingClock = setInterval(refresh, 1);

  var player = this.player = {};
  player.x;
  player.y;
  player.lastX;
  player.lastY;
  player.bomb = 0;
  player.status = true;
  player.model = flyimages["me"];
  player.model2 = flyimages["me_2"];
  player.width = player.model.width;
  player.height = player.width / player.model.width * player.model.height;
  player.move = function (x, y) {
    player.lastX = player.x;
    player.lastY = player.y;
    player.x = x - player.width / 2;
    player.y = y;
    player.x = player.x > c_width - player.width ? c_width - player.width : player.x;
    player.x = player.x < 0 ? 0 : player.x;
    player.y = player.y > c_height - player.height * 3 / 2 ? c_height - player.height * 3 / 2 : player.y - player.height * 0.5;
    player.y = player.y < 0 ? 0 : player.y;
  }
  player.moveing = function () {
    if (!player.status) {
      return;
    }

    cxt.drawImage(game.time % 30 > 15 ? player.model.src : player.model2.src, player.x, player.y, player.width, player.height);
    player.attacking();
  }
  player.cartridges = [];
  player.attackTime = 0;
  player.attackPower = false;
  player.attack = function () {
    if (!player.status) {
      return;
    }

    player.attackTime++;
    if ((player.attackTime * game.refreshInterval) % (game.refreshInterval * 20) != 0) {
      return;
    }

    player.attackTime = 0;
    //playAudio( "fire_bullet.mp3" );
    var cartridges;
    if (player.attackPower) {
      cartridges = [(new cartridge(player.x - (player.width / 5), player.y, 2, player)), (new cartridge(player.x + (player.width / 5), player.y, 2, player))];
    } else {
      cartridges = [(new cartridge(player.x, player.y, 1, player))];
    }
    Array.prototype.push.apply(player.cartridges, cartridges);
  }
  player.attacking = function () {
    player.attack();
    var cartridgeSpeed = config.cartridgeSpeed;
    var cartridges_length = player.cartridges.length;
    firstloop: for (var i = cartridges_length; i--;) {
      var cartridge = player.cartridges[i];
      cxt.drawImage(cartridge.model.src, cartridge.x, cartridge.y, cartridge.width, cartridge.height);
      if (cartridge.y <= 0) {
        player.cartridges.splice(i, 1);
        continue firstloop;
      }

      var plain_length = game.plains.length;
      secondloop: for (var j = plain_length; j--;) {
        var plain = game.plains[j];
        var X = cartridge.x;
        var Y = cartridge.y;
        var nextY = Y - cartridgeSpeed;
        if (
          X > plain.x
          && X < (plain.x + plain.width)
          && nextY < (plain.y + plain.height + plain.speed)
          && Y >= (plain.y + plain.height)
        ) {
          plain.byAttack();
          player.cartridges.splice(i, 1);
          continue firstloop;
        }
      }

      cartridge.y = cartridge.y - cartridgeSpeed;  //子弹向上移动
    }
  }
  player.useBomb = function () {
    if (game.player.bomb <= 0) {
      return;
    }
    game.player.bomb--;
    //playAudio( "use_bomb.mp3" );
    var plains_length = game.plains.length;
    for (var i = plains_length; i--;) {
      var plain = game.plains[i];
      plain.die();
    }
  }
  player.die = function () {
    if (!player.status) {
      return;
    }

    player.status = false;
    //playAudio( "game_over.mp3" );
    var dieSpeed = 20;
    var x = player.x;
    var y = player.y;
    var h = player.height;
    var w = player.width;

    game.plainsDies.push((new playerDie()));

    function playerDie() {
      var dieTime = 4 * dieSpeed;
      this.animationTime = 4 * dieSpeed;

      this.call = function () {
        if (this.animationTime == 1) {
          isEnd = 0
          game.over();
        }
        var dieModel = flyimages["me_die" + (parseInt((dieTime - this.animationTime) / dieSpeed) + 1) + ""];
        cxt.drawImage(dieModel.src, x, y, w, h);
        this.animationTime--;
      }
    }
  }

  var game = this.game = {};
  game.fire = this.__proto__.fire;
  game.score = 0;
  game.time = 0;
  game.redbag = 0;
  game.gold = 0;
  game.player = player;
  game.bgImg = flyimages["bg"];
  game.refreshInterval = config.gameSpeed;
  game.refresh = function () {
    // console.log('我来自plane')
    game.time++;
    game.bgScroll();
    game.plainsScroll();
    game.plainsDying();
    game.player.moveing();
    game.propShow();
    game.refreshMessage();
    wx.drawCanvas({
      canvasId: id,
      actions: cxt.getActions()
    })
  }
  game.bgScrollTime = 0;
  game.bgScroll = function () {
    var bg_img_height = game.bgImg.height;
    var bg_img_width = game.bgImg.width;
    game.bgScrollTime += 0.5;
    if (game.bgScrollTime > bg_img_height - 1) {
      game.bgScrollTime = 0;
    }
    cxt.drawImage(game.bgImg.src, 0, game.bgScrollTime - bg_img_height + 1, bg_img_width, bg_img_height);
    cxt.drawImage(game.bgImg.src, 0, game.bgScrollTime, bg_img_width, bg_img_height);
  }
  game.props = [];
  game.addProp = function () {
    var interval = 18;
    if ((game.time * game.refreshInterval) % (interval * 1000) == 0) {
      // game.props.push((new prop(parseInt(Math.random() * 1.8 + 1.1))));
      let index = parseInt(Math.random() * 2.8 + 1.1)
      if (index == 1) {
        game.props.push((new prop(1)));
      } else if (index == 2) {
        game.props.push((new prop(2)));
      } else if (index == 3) {
        // (game.time * game.refreshInterval) % (interval * 1000 * 4) == 0 && 
        if (game.gold < goldNum) {
          game.props.push((new prop(4)));
        } else {
          game.props.push((new prop(2)));
        }
      }
  }
    if ((game.time) % 4000 == 0) {
      if (game.gold < redNum) {
        audios.pause()
        setTimeout(() => {
          audios.play()
        }, 2000)
        getRed()
        game.props.push((new prop(3)));
      } else {
        game.props.push((new prop(1)));
      }
    }
  }
  game.propShow = function () {
    game.addProp();
    var props_length = game.props.length;
    for (var i = props_length; i--;) {
      var prop = game.props[i];
      if (prop.isDeleted == true) {
        game.props.splice(i, 1);
        continue;
      }

      prop[prop.status]();

      if (prop.y > c_height) {
        game.props.splice(i, 1);
        continue;
      }
    }
  }
  game.plains = [];
  game.plainsNum = 0;
  game.addPlain = function () {
    if (game.time % 60 != 0) {
      return;
    }

    if (game.plainsNum == 36) {
      game.plainsNum = 0;
    }
    console.log(game.time)
    game.plainsNum++;
    switch (true) {
      case game.time % 3000 == 0:
        getBoss()
        audios.pause()
        setTimeout(() => {
          audios.play()
        }, 6000)
        game.plains.push(new plain(8));
        break;
      case game.plainsNum % 6 == 0:
        game.plains.push(new plain(3));
        break;
      case game.plainsNum % 3 == 0:
        game.plains.push(new plain(2));
        break;
      default:
        // game.plains.push(new plain(1));
        let index = Math.random()
        if (index <= 0.2) {
          game.plains.push(new plain(1));
        } else if (index <= 0.4) {
          game.plains.push(new plain(5));
        } else if (index <= 0.6) {
          game.plains.push(new plain(6));
        } else {
          game.plains.push(new plain(7));
        }
        break;
    }

  }
  game.plainsScroll = function () {
    game.addPlain();
    var removePlain = [];
    var plains_length = game.plains.length;
    for (var i = plains_length; i--;) {
      var plain = game.plains[i];
      if (plain.y > c_height || plain.status == false) {
        game.plains.splice(i, 1);
        continue;
      }

      plain.show();

      if (isCollide(plain)) {
        game.player.die();
      }

      plain.y = plain.y + plain.speed;
    }

    //判断是否和玩家的飞机碰撞
    function isCollide(plain) {
      var plainTopLeft = [plain.x, plain.y];
      var plainBottomRight = [plain.x + plain.width, plain.y + plain.height];
      var meTopLeft = [game.player.x + game.player.width / 3, game.player.y];
      var meBottomRight = [game.player.x + (game.player.width * 2 / 3), game.player.y + (game.player.height * 2 / 3)];

      var collideTopLeft = [Math.max(plainTopLeft[0], meTopLeft[0]), Math.max(plainTopLeft[1], meTopLeft[1])];
      var collideBottomRight = [Math.min(plainBottomRight[0], meBottomRight[0]), Math.min(plainBottomRight[1], meBottomRight[1])];

      if (collideTopLeft[0] < collideBottomRight[0] && collideTopLeft[1] < collideBottomRight[1]) {
        return true;
      }

      return false;
    }
  }
  game.plainsDies = [];
  game.plainsDying = function () {
    var plainsDies_length = game.plainsDies.length;
    for (var i = plainsDies_length; i--;) {
      var plainDie = game.plainsDies[i];
      if (plainDie.animationTime == 0) {
        game.plainsDies.splice(i, 1);
        continue;
      }
      plainDie.call();
    }
  }
  game.over = function () {
    click_url = ''
    audios.destroy()
    audios = null
    // cxt.clearRect(0, 0, width, 100 * vh)
    clearInterval(game.clock);
    // game.clear()
    game.fire('over', { score: score, red: red_number, gold: gold_number, isEnd: isEnd });
    // flyGame = null
  }
  game.clear = function () {
    game.player.x = (c_width - game.player.width) / 2;
    game.player.y = c_height - game.player.height * 3 / 2;

    game.plains = [];
    game.plainsDies = [];
    game.plainsNum = 0;
    game.time = 0;
    game.bgScrollTime = 0;
    game.score = 0;
    game.player.status = true;
    game.player.bomb = 0;
    game.player.attackPower = false;
    clearTimeout(game.player.attackPowerClock);
    clearInterval(game.clock);
  }
  game.start = function () {
    game.clear();
    game.clock = setInterval(function () {
      game.refresh();
    }, game.refreshInterval);
  }
  game.refreshMessage = function () {
    cxt.setFontSize(4.26 * unit)
    cxt.setFillStyle('#fff')
    // game.getscore()
    cxt.fillText('得分：', 4.26 * unit, 10 * vh);
    cxt.setFillStyle('#FFCB1F')
    cxt.fillText(game.score + '分', 16.26 * unit, 10 * vh);
    cxt.setFillStyle('#fff')
    cxt.fillText('红包：', 4.26 * unit, 14 * vh);
    cxt.setFillStyle('#FFCB1F')
    cxt.fillText(parseFloat(game.redbag).toFixed(2) + '元', 16.26 * unit, 14 * vh);
    cxt.setFillStyle('#fff')
    cxt.fillText('金币', 4.26 * unit, 18 * vh);
    cxt.setFillStyle('#FFCB1F')
    cxt.fillText(game.gold + '个', 16.26 * unit, 18 * vh);
  }

  function prop(type) {
    this.type = type;
    this.status = "show";
    this.isDeleted = false;
    this.modelImg;
    this.getSound;
    switch (type) {
      case 1:
        this.modelImg = "prop1";
        // this.getSound = "get_bomb.mp3";
        break;
      case 2:
        this.modelImg = "prop2";
        // this.getSound = "get_double_laser.mp3";
        break;
      case 3:
        this.modelImg = "prop3";
        // this.getSound = "https://resource.160nurse.com/canvas/healthy/mp3/get_thing.mp3";
        break;
      case 4:
        this.modelImg = "prop4";
        // this.getSound = "https://resource.160nurse.com/canvas/healthy/mp3/get_thing.mp3";
        break;
    }
    this.model = flyimages[this.modelImg];
    this.width = this.model.width;
    this.height = this.model.height / this.model.width * this.width;
    this.x = Math.random() * (c_width - this.width);
    this.y = -this.height;

    var speed = this.speed = 6;
    var animateTime = this.animateTime = 70;
    this.showType = "down";
    this.show = function () {
      if (this.animateTime <= animateTime / 2) {
        this.showType = "up";
      }
      cxt.drawImage(this.model.src, this.x, this.y, this.width, this.height);
      if (isGain(this)) {
        this.isDeleted = true;
        this.byGain();
        return;
      }
      var move = ((c_height + this.height) / 3) / (animateTime / 2);
      this.speed = move;
      if (this.showType == "down") {
        this.y += move;
      } else {
        this.y -= move;
      }
      this.animateTime--;
      if (this.animateTime <= 0) {
        this.speed = speed;
        this.status = "move";
      }
    }
    this.move = function () {
      this.y += this.speed;
      cxt.drawImage(this.model.src, this.x, this.y, this.width, this.height);
      if (isGain(this)) {
        this.isDeleted = true;
        this.byGain();
        return;
      }
    }

    this.byGain = function () {
      switch (this.type) {
        case 1:
          getBomb()
          game.player.bomb++;
          game.player.useBomb()
          break;
        case 2:
          getDan()
          game.player.attackPower = true;
          game.player.attackPowerClock = setTimeout(function () {
            game.player.attackPower = false;
          }, 15000);
          break;
        case 3:
          // getGolds();
          let index = Math.random()
          let money = 0
          if (index <= 0.65) {
            money = 0.02
          } else if (index <= 0.8) {
            money = 0.05
          } else if (index <= 0.9) {
            money = 0.08
          } else if (index <= 0.95) {
            money = 0.1
          } else if (index <= 0.99) {
            money = 0.2
          } else if (index <= 1) {
            money = 0.3
          }
          red_number += money
          game.redbag += money
          break;
        case 4:
          getGolds();
          gold_number++
          game.gold++
          break;
      }
      //playAudio( this.getSound );
    }

    //判断有没有吃到道具
    var isGain = function (prop) {
      var leftX = prop.x;
      var rightX = prop.x + prop.width;
      if (rightX < game.player.x || leftX > (game.player.x + game.player.width)) {
        return false;
      }
      var removing = prop.status == "move" ? prop.speed : (prop.showType == "down" ? prop.speed : -prop.speed);
      var nextY = prop.y + removing;
      if (((prop.y + prop.height) > game.player.y || (nextY + prop.height) < game.player.y) && game.player.lastY > (prop.y + prop.height)) {
        return false;
      }
      return true;
    }
  }

  function plain(type) {
    this.type = type;
    this.hp;  //飞机生命值
    this.height;
    this.width;
    this.maxSpeed;
    this.dieTime;
    this.status = true;  //飞机死了没
    var dieSpeed = 20;  //死亡动画播放速度

    switch (type) {
      case 1:
        this.hp = 1;
        this.score = 10;
        this.maxSpeed = 5;
        this.dieTime = dieSpeed * 3;
        break;
      case 2:
        this.hp = 3;
        this.score = 50;
        this.maxSpeed = 4;
        this.dieTime = dieSpeed * 3;
        break;
      case 3:
        this.hp = 6;
        this.score = 100;
        this.maxSpeed = 3;
        this.dieTime = dieSpeed * 4;
        break;
      case 4:
        this.hp = 1;
        this.score = 10;
        this.maxSpeed = 8;
        this.dieTime = dieSpeed * 3;
        break;
      case 5:
        this.hp = 1;
        this.score = 10;
        this.maxSpeed = 8;
        this.dieTime = dieSpeed * 3;
        break;
      case 6:
        this.hp = 1;
        this.score = 10;
        this.maxSpeed = 8;
        this.dieTime = dieSpeed * 3;
        break;
      case 7:
        this.hp = 1;
        this.score = 10;
        this.maxSpeed = 8;
        this.dieTime = dieSpeed * 3;
        break;
      case 8:
        this.hp = 40;
        this.score = 500;
        this.maxSpeed = 0.05;
        this.dieTime = dieSpeed * 6;
        break;
    }

    // this.dieSound = "plain" + this.type + "_die.mp3";
    this.modelimg = "plain" + this.type + "";
    this.model = flyimages[this.modelimg];

    if (this.type == 3) {
      this.modelimg2 = "plain3_2";
      this.model2 = flyimages[this.modelimg2];
    }

    this.width = this.model.width;
    this.height = this.width / this.model.width * this.model.height;

    this.x = Math.random() * (c_width - this.width);
    this.y = -(this.height);

    var maxSpeed = game.time / 1000 > 10 ? 10 : game.time / 1000;
    this.speed = Math.random() * (maxSpeed - 1) + 1;
    if (this.type == 8) {
      this.speed = 0.4
    } else {
      if (game.time / 1000 <= 60) {
        this.speed = 1 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 120) {
        this.speed = 2 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 180) {
        this.speed = 3 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 240) {
        this.speed = 3.5 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 300) {
        this.speed = 4 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 360) {
        this.speed = 4.5 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 420) {
        this.speed = 5 < this.maxSpeed ? this.speed : this.maxSpeed
      } else if (game.time / 1000 <= 480) {
        this.speed = 6 < this.maxSpeed ? this.speed : this.maxSpeed
      }
    }

    this.show = function () {
      if (this.type == 3) {
        cxt.drawImage(game.time % 30 > 15 ? this.model.src : this.model2.src, this.x, this.y, this.width, this.height);
        return;
      }
      cxt.drawImage(this.model.src, this.x, this.y, this.width, this.height);
    }

    this.die = function () {
      var plainType = this.type;
      var plainX = this.x;
      var plainY = this.y;
      var plainW = this.width;
      var plainH = this.height;

      game.plainsDies.push((new die(this.dieTime)));

      game.score += this.score;
      score +=this.score
      this.status = false;

      function die(dieTime) {
        var dieTime = dieTime;
        this.animationTime = dieTime;

        this.call = function () {
          if (this.animationTime <= 0) {
            return;
          }
          var dieModel = flyimages["plain" + plainType + "_die" + (parseInt((dieTime - this.animationTime) / dieSpeed) + 1) + ""];
          cxt.drawImage(dieModel.src, plainX, plainY, plainW, plainH);
          this.animationTime--;
        }
      }
    }

    var hp = this.hp;
    this.byAttack = function () {
      this.hp--;
      if (this.hp <= 0) {
        this.die();
        playAudio()
        //playAudio( this.dieSound );
        return;
      }
      playAudio()
      if (this.hp <= hp / 2) {
        this.model = flyimages["plain" + this.type + "_hurt"];
      }
    }
  }

  function cartridge(x, y, type, player) {
    this.model = flyimages[type == 2 ? "cartridge_power" : "cartridge"];

    this.width = this.model.width;
    this.height = this.model.height;
    this.x = x + (player.width - this.width) / 2;
    this.y = y - this.height;
  }
}

// flyGame.prototype.startGame = function () {
//   this.game.start();
// }

flyGame.prototype.touchmove = function (x, y) {
  this.game.player.move(x, y);
}

flyGame.prototype.over = function () {
  isEnd = 1
  // this.game.clear()
  this.game.over();
}

emitter.setup(flyGame.prototype);

module.exports = flyGame